package org.lql.algo.codecrush.hot100.linked;

import org.lql.algo.common.ListNode;

/**
 * @author: liangqinglong
 * @date: 2025-10-11 13:55
 * @description: 206. 反转链表 <a href="https://leetcode.cn/problems/reverse-linked-list/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class ReverseList {

	public ListNode reverseList(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		}
		ListNode last = null;
		while (head != null) {
			ListNode next = head.next;
			head.next = last;
			last = head;
			head = next;
		}
		return last;
	}

	public static void main(String[] args) {
		ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
		ListNode listNode = new ReverseList().reverseList(head);
		while (listNode != null) {
			System.out.print(listNode.val + " -> ");
			listNode = listNode.next;
		}
	}
}
